package cn.darksoul3.spark.dataset

import org.apache.spark.sql.{Dataset, Row, SparkSession}

object WCWithDataSetBySQL {

  def main(args: Array[String]): Unit = {

    val ss: SparkSession = SparkSession.builder().appName("WCWithDataSet").master("local[*]").getOrCreate()
    val lines: Dataset[String] = ss.read.textFile("C:\\Users\\cary2\\Desktop\\wordcount.txt")
    import ss.implicits._
    val words: Dataset[String] = lines.flatMap(_.split("\\s+"))

    //words.printSchema()
    //words.show()

    words.createTempView("v_wc")
    val res: Dataset[Row] = ss.sql("select value,count(*) counter from v_wc group by value order by counter desc")
    res.show()

    ss.stop()
  }

}
